extends "res://LevelManager.gd"

# 森林关卡特有属性
@export var rain_chance: float = 0.15  # 雨天触发几率
@export var rain_duration: float = 15.0  # 雨天持续时间
@export var rain_healing: int = 1  # 雨天每秒治疗量
@export var fog_chance: float = 0.1  # 雾天触发几率
@export var fog_duration: float = 12.0  # 雾天持续时间
@export var mushroom_chance: float = 0.08  # 蘑菇生成几率
@export var mushroom_effect_duration: float = 10.0  # 蘑菇效果持续时间

# 天气计时器
var weather_timer: float = 0.0
var is_rain_active: bool = false
var rain_active_timer: float = 0.0
var is_fog_active: bool = false
var fog_active_timer: float = 0.0

# 蘑菇引用
var mushrooms = []

func _ready():
	# 调用父类的_ready方法
	super._ready()
	
	# 设置关卡类型
	current_level_type = LevelType.FOREST
	
	# 根据难度调整参数
	adjust_difficulty()
	
	# 添加到关卡管理器组
	add_to_group("level_manager")
	
	# 设置背景
	var background_node = get_node_or_null("Background")
	if background_node:
		background_node.texture = load("res://asset/background/forest/image_fx_.jpg")
	
	# 初始化敌人生成器
	var enemy_spawner_node = get_node_or_null("EnhancedEnemySpawner")
	if enemy_spawner_node:
		enemy_spawner_node.level_type = LevelType.FOREST
		enemy_spawner_node.difficulty = level_difficulty
		enemy_spawner_node.start_spawning()
	
	# 显示关卡信息
	print("森林关卡 - 难度:", level_difficulty)

func _process(delta):
	# 调用父类的_process方法
	super._process(delta)
	
	# 处理天气逻辑
	if not is_rain_active and not is_fog_active:
		weather_timer += delta
		if weather_timer >= 40.0:  # 每40秒检查一次
			weather_timer = 0.0
			
			# 随机选择天气
			var weather_roll = randf()
			if weather_roll < rain_chance:
				trigger_rain()
			elif weather_roll < rain_chance + fog_chance:
				trigger_fog()
	
	# 处理雨天逻辑
	if is_rain_active:
		rain_active_timer += delta
		
		# 雨天治疗
		if int(rain_active_timer) > int(rain_active_timer - delta):  # 每秒触发一次
			if player and is_instance_valid(player):
				player.heal(rain_healing)
		
		# 雨天结束
		if rain_active_timer >= rain_duration:
			end_rain()
	
	# 处理雾天逻辑
	if is_fog_active:
		fog_active_timer += delta
		
		# 雾天结束
		if fog_active_timer >= fog_duration:
			end_fog()
	
	# 处理蘑菇生成逻辑
	if randf() < mushroom_chance * delta:
		spawn_mushroom()
	
	# 清理已销毁的蘑菇
	var active_mushrooms = []
	for mushroom in mushrooms:
		if is_instance_valid(mushroom):
			active_mushrooms.append(mushroom)
	mushrooms = active_mushrooms

func adjust_difficulty():
	# 根据难度调整参数
	match level_difficulty:
		1:
			level_time_limit = 180.0  # 3分钟
			rain_chance = 0.2
			fog_chance = 0.05
		2:
			level_time_limit = 210.0  # 3.5分钟
			rain_chance = 0.15
			fog_chance = 0.1
		3:
			level_time_limit = 240.0  # 4分钟
			rain_chance = 0.15
			fog_chance = 0.15
		4:
			level_time_limit = 270.0  # 4.5分钟
			rain_chance = 0.1
			fog_chance = 0.2
		5:
			level_time_limit = 300.0  # 5分钟
			rain_chance = 0.1
			fog_chance = 0.25

func trigger_rain():
	is_rain_active = true
	rain_active_timer = 0.0
	
	# 创建雨天效果
	var rain = preload("res://RainEffect.tscn").instantiate()
	add_child(rain)
	rain.global_position = Vector2(get_viewport_rect().size.x / 2, 0)
	rain.duration = rain_duration
	
	# 通知UI
	if ui:
		ui.show_message("开始下雨了！")
	
	print("雨天开始!")

func end_rain():
	is_rain_active = false
	
	# 通知UI
	if ui:
		ui.show_message("雨停了")
	
	print("雨天结束!")

func trigger_fog():
	is_fog_active = true
	fog_active_timer = 0.0
	
	# 创建雾天效果
	var fog = preload("res://FogEffect.tscn").instantiate()
	add_child(fog)
	fog.global_position = Vector2(get_viewport_rect().size.x / 2, get_viewport_rect().size.y / 2)
	fog.duration = fog_duration
	
	# 减少敌人视野
	var enemies = get_tree().get_nodes_in_group("enemies")
	for enemy in enemies:
		if enemy.has_method("reduce_vision_range"):
			enemy.reduce_vision_range(0.5)  # 减少50%视野
	
	# 通知UI
	if ui:
		ui.show_message("浓雾出现！")
	
	print("雾天开始!")

func end_fog():
	is_fog_active = false
	
	# 恢复敌人视野
	var enemies = get_tree().get_nodes_in_group("enemies")
	for enemy in enemies:
		if enemy.has_method("restore_vision_range"):
			enemy.restore_vision_range()
	
	# 通知UI
	if ui:
		ui.show_message("浓雾散去")
	
	print("雾天结束!")

func spawn_mushroom():
	# 创建蘑菇
	var mushroom = preload("res://MushroomEffect.tscn").instantiate()
	add_child(mushroom)
	
	# 设置蘑菇位置（随机位置，但不要太靠近边缘）
	var viewport_size = get_viewport_rect().size
	var x = randf_range(100, viewport_size.x - 100)
	var y = randf_range(100, viewport_size.y - 100)
	mushroom.global_position = Vector2(x, y)
	
	# 设置蘑菇属性
	var mushroom_type = randi() % 3  # 0=攻击, 1=速度, 2=生命
	mushroom.effect_type = mushroom_type
	mushroom.effect_duration = mushroom_effect_duration
	mushroom.effect_strength = 1.0 + (level_difficulty * 0.1)  # 根据难度增加效果强度
	
	# 添加到蘑菇列表
	mushrooms.append(mushroom)
	
	print("蘑菇出现!")
	
	# 设置蘑菇消失计时器
	var timer = get_tree().create_timer(30.0)
	timer.connect("timeout", _on_mushroom_timeout.bind(mushroom))

func _on_mushroom_timeout(mushroom):
	if mushroom and is_instance_valid(mushroom):
		mushroom.queue_free()
		
		print("蘑菇消失!")

# 重写完成关卡方法，添加森林特有的完成条件
func complete_level():
	# 检查是否击败了足够的敌人
	var enemy_spawner_node = get_node_or_null("EnhancedEnemySpawner")
	if enemy_spawner_node:
		var enemies_defeated = enemy_spawner_node.enemies_defeated
		var required_defeats = 60 + (level_difficulty * 12)  # 根据难度调整所需击败敌人数
		
		if enemies_defeated >= required_defeats:
			# 调用父类的完成方法
			super.complete_level()
		elif level_timer >= level_time_limit:
			# 时间到但没有击败足够的敌人
			fail_level()
	else:
		# 如果没有敌人生成器，使用默认完成逻辑
		super.complete_level()
